﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CustomsDocumentOA.Project.CusDocument.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CustomsDocumentOA.Project.CusDocument
{
    public class CustomerAppservice : AsyncCrudAppService<CDOA_Customer, CustomerDto, long, PagedResultRequestDto, UpdateCustomerDto, UpdateCustomerDto>, ICustomerAppservice
    {
        private readonly IObjectMapper _objectMapper;
        private readonly ICustomerDetailAppservice _customerDetail;
        public CustomerAppservice(IRepository<CDOA_Customer, long> repository, IObjectMapper objectMapper, ICustomerDetailAppservice customerDetail) : base(repository)
        {
            _objectMapper = objectMapper;
            _customerDetail = customerDetail;
        }
        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="CurrentPage">当前页码</param>
        /// <param name="PageSizes">页数</param>
        /// <param name="customeName">企业名称</param>
        /// <param name="creatorUserId">创建人</param>
        /// <param name="cCode">企业代码</param>
        /// <param name="cBoss">企业法人</param>
        /// <returns></returns>
        public async Task<PagedResultDto<CustomerDto>> GetList(int CurrentPage, int PageSizes,string customeName = "",long creatorUserId = 0,string cCode = "",string cBoss = "")
        {
            PagedResultDto<CustomerDto> resultDto = new PagedResultDto<CustomerDto>();
            List<CDOA_Customer> lists = new List<CDOA_Customer>();
            Expression<Func<CDOA_Customer, bool>> expr = o => o.Id > 0;
            if (creatorUserId > 0)
            {
                expr = expr.ExpressionAnd(o => o.CreatorUserId == creatorUserId);
            }
            if (!string.IsNullOrEmpty(customeName))
            {
                expr = expr.ExpressionAnd(o => o.cCustomerName.Contains(customeName));
            }
            if (!string.IsNullOrEmpty(cCode))
            {
                expr = expr.ExpressionAnd(o => o.cCode.Contains(cCode));
            }
            if (!string.IsNullOrEmpty(cBoss))
            {
                expr = expr.ExpressionAnd(o => o.cBoss.Contains(cBoss));
            }
            lists = await Repository.GetAll().Where(expr).ToListAsync();
            lists = lists.OrderBy(o => o.CreationTime).ToList();
            var dtos = lists.Skip((CurrentPage - 1) * PageSizes).Take(PageSizes).ToList();
            resultDto.Items = _objectMapper.Map<List<CustomerDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion

        #region 获取客户详细信息
        /// <summary>
        /// 获取客户详细信息
        /// </summary>
        /// <param name="customerId">id</param>
        /// <returns></returns>
        public async Task<CusDetails> GetCustomerInfo(int customerId) 
        {
            CusDetails details = new CusDetails();
            var obj = await Repository.FirstOrDefaultAsync(o => o.Id == customerId);
            var lists = await _customerDetail.GetCustomerDetails(customerId);
            details.Details = lists;
            details.cAddress = obj.cAddress;
            details.cBoss = obj.cAddress;
            details.cCode = obj.cCode;
            details.cCustomerName = obj.cCustomerName;
            details.cDescribe = obj.cDescribe;
            details.cEstablishTime = obj.cEstablishTime;
            details.CreationTime = obj.CreationTime;
            details.CreatorUserId = obj.CreatorUserId;
            return details;
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        #region 批量删除
        //public async Task DeleteList(List<long> id)
        //{
        //    await Repository.DeleteAsync(it => id.Contains(it.Id));
        //}
        #endregion
        public async Task DeleteList(string id)
        {
            string[] obj = id.Split(",");
            for (int i = 0; i < obj.Length; i++)
            {
                await Repository.DeleteAsync(long.Parse(obj[i]));
            }
        }
        #endregion
    }
}
